package com.mayikt.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;

/**
 * @Classname UrlUtils
 * @Description SpringBoot SpringCloud 构建微服务电商平台。
 * @Created by CHEN JIN LONG
 * @Date 2019/1/22 16:36
 * @Version 1.0
 */
@Slf4j
public class UrlUtils {


    //一般国际采用utf-8
    //private final static String ENCODE = "UTF-8";
    //国内标准GBK
    private final static String ENCODE = "GBK";

    /**
     * URL 解码
     *
     * @param str
     * @return
     */
    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            log.error("URL解码失败 ex={}", e.getMessage(), e);
        }
        return result;
    }

    /**
     * URL 转码
     *
     * @param str
     * @return
     */
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            log.error("URL转码失败 ex={}", e.getMessage(), e);
        }
        return result;
    }
}
